<script lang="ts" setup>
import BaseButton from "@/components/BaseButton/index.vue";
import BaseUpDown from "@/components/BaseUpDown/index.vue";
import DateRange from "@/components/DateRange/index.vue";
import useSearch from "@/hooks/useSearch";
import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";

const { pageData, roleMap } = useData();
import { getDataByPage ,exportData} from "@/api/modules/examiner/upgrade-log";
import UpdateForm from "./components/UpdateForm/index.vue";
import UpdateFormPassword from "./components/UpdateFormPassword/index.vue";

const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const { tableData, loadData, formData, reloadData } = useSearch({
  defaultData: {},
  responseKey: {
    list: "data",
    total: "total"
  }
});

function loadDataCb() {
  loadData({
    api: getDataByPage,
    sendData: {

      curpage: formData.pageNum - 1,
      perpage: formData.pageSize,
      ip: formData.ip,
      detail: formData.detail,
      start_time: formData.start_time ? parseInt(new Date(formData.start_time).getTime() / 1000) : "",
      end_time: formData.end_time ? parseInt(new Date(formData.end_time).getTime() / 1000) : ""
    }
  });
}

loadDataCb();

function reloadDataCb() {
  reloadData();
  loadData({
    api: getDataByPage,
    sendData: {
      curpage: formData.pageNum - 1,
      perpage: formData.pageSize,
    }
  });
}

loadDataCb();

/**删除*/
import useDelete from "@/hooks/useDelete";
import { computed } from "vue";
import BaseCollapse from "@/components/BaseCollapse/index.vue";
import moment from "moment";
import { downloadTemplate } from "@/api/modules/resource/host-finger";
import { ElMessage } from "element-plus";

const { deleteHandler, idList } = useDelete();

async function deleteCb(id) {
  await deleteHandler({
    api: deleteData,
    multiple: false,
    sendData: {
      id: [id]
    },
    hideMessageBox: false
  });
  loadDataCb();
}

function selectCb(data) {
  console.log(data);
  idList.value = data.rowList.map(item => item.userId);
}
async function templateOperate(type) {
  const urlDownload = function (url, name) {
    var a = document.createElement("a");
    // console.log(url)
    a.setAttribute("href", url);
    a.setAttribute("download", name);
    a.setAttribute("target", "_blank");
    const clickEvent = document.createEvent("MouseEvents");
    clickEvent.initEvent("click", true, true);
    a.dispatchEvent(clickEvent);
  };
  if (type === "export") {
    const res = await exportData();
    console.info(res);
    const url = URL.createObjectURL(res);
    urlDownload(url, `operation_log${moment().format("YYYYMMDDHHmmss")}.csv`);
  }
  if (type === "download") {
    const res = await downloadTemplate();
    console.info(res);
    const url = URL.createObjectURL(res);
    urlDownload(url, `finger_template_${moment().format("YYYYMMDDHHmmss")}.csv`);
  }
  if (type === "import") {
    const tempInput = document.createElement("input");
    tempInput.type = "file";
    tempInput.accept = ".xls,.xlsx,.csv";
    tempInput.click();
    tempInput.addEventListener("change", async () => {
      const res = await importData({ file: tempInput.files[0], ifid: formData.ifid });
      if (res.success) {
        ElMessage.success("导入成功");
        loadDataCb();
      } else {
        ElMessage.warning(res.msg);
      }
    });
  }
  // this.waitOperator();
}
</script>

<template>
  <BaseUpDown>
    <template #up>
      <BaseCollapse title="">
        <template #header>
        </template>
        <div class="h-box">
          <el-form-item label="IP">
            <el-input v-model="formData.ip" style="width: 120px" placeholder="请输入" />
          </el-form-item>
          <el-form-item class="mg-l-sm" label="详情">
            <el-input v-model="formData.detail" style="width: 120px" placeholder="请输入" />
          </el-form-item>
          <el-form-item class="mg-l-sm" label="时间">
            <date-range width="220px" v-model:start="formData.startTime" v-model:end="formData.endTime"></date-range>
          </el-form-item>
          <div class="mg-l-sm" style="margin-bottom: 18px">
            <base-button @click="loadDataCb" type="search_icon_text"></base-button>
            <base-button @click="reloadDataCb" type="refresh_icon_text"></base-button>
          </div>
        </div>
      </BaseCollapse>
    </template>
    <he-table
      @handle-current-change="loadDataCb"
      :total="tableData.total"
      v-model:page-size="formData.pageSize"
      v-model:page-num="formData.pageNum"
      operate-width="180px"
      :operate-list="[]"
      :table-data="tableData.list"
      :column-data="pageData.columnData"
      :show-multiple="false"
      :show-summary="false"
      :show-operate="false"
      :show-multiple-column="false"
      :show-pagination="true"
      @select="selectCb"
    >
      <template #operationDefault="{ data }">
        <UpdateForm
          :row-data="{
            id: data.row[data.row.length - 1],
            name: data.row[1],
            role: roleMap.get(data.row[2])
          }"
          :title="`编辑用户:${data.row[1]}`"
          @success="loadDataCb"
        >
          <base-button type="edit_text"></base-button>
        </UpdateForm>
        <UpdateFormPassword
          :row-data="{
            id: data.row[data.row.length - 1],
            name: data.row[1],
            role: roleMap.get(data.row[2])
          }"
          :title="`重置密码:${data.row[1]}`"
          @success="loadDataCb"
        >
          <el-button type="primary" link>重置密码</el-button>
        </UpdateFormPassword>
        <base-button
          v-if="username != data.row[1]"
          @click="deleteCb(data.row[data.row.length - 1])"
          type="del_text"
        ></base-button>
      </template>
    </he-table>
  </BaseUpDown>
</template>
